package com.neouton.crm.pm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.paymentPlan.entity.PaymentPlan;
import com.neouton.crm.paymentPlan.service.PaymentPlanService;
import com.neouton.crm.pm.entity.PurchaseOrderProductVO;
import com.neouton.crm.pm.entity.PurchaseOrderVO;
import com.neouton.crm.pm.service.IPurchaseOrdeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合同相关controller
 * @Author libingkun
 * @Date 2022年11月30日
 * @Version 1.0
 *          初始化迁移
 */
@RestController
@RequestMapping("purchaseOrde")
public class PurchaseOrdeController {

    @Resource
    private IPurchaseOrdeService purchaseOrdeService;

    @Resource
    private PaymentPlanService paymentPlanService;

    /**
     * 保存采购信息
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/saveOrUpdatePurchaseOrde")
    public ResponseAPI savePurchasesoInfo(@RequestBody PurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        purchaseOrder.setCreator(AuthUtils.getUserId());
        purchaseOrder.setLastUpdateBy(AuthUtils.getUserId());
        return ResponseAPI.successData(purchaseOrdeService.saveOrUpdatePurchaseOrder(purchaseOrder,token));
    }

    /**
     * 修改项目
     *
     * @param purchaseOrderVO
     * @return
     */
    @PostMapping("/updatePurchaseOrdeAfterPass/")
    public ResponseAPI updatePurchaseOrdeAfterPass(@RequestBody PurchaseOrderVO purchaseOrderVO, @RequestHeader("token") String token) {
        purchaseOrdeService.updatePurchaseOrdeAfterPass(purchaseOrderVO,token);
        return  ResponseAPI.successData("success");
    }


    @PostMapping("/queryPurchaseOrdeFromCache/{purchaseId}")
    public ResponseAPI queryPurchaseOrdeFromCache(@PathVariable(value = "purchaseId") int purchaseId, @RequestHeader("token") String token) {
        PurchaseOrderVO purchaseOrderVO = purchaseOrdeService.queryPurchaseOrdeFromCache(purchaseId, token);
        return ResponseAPI.successData(purchaseOrderVO);
    }

    /**
     * 获取当日最大采购单号
     * @return
     */
    @PostMapping("/getPurchaseOrdeMaxNumByDate")
    public ResponseAPI getPurchaseOrdeMaxNumByDate(){
        String maxNum = purchaseOrdeService.getPurchaseOrdeMaxNumByDate();
        return ResponseAPI.successData(maxNum);
    }

    /**
     * 更具采购单号获取采购信息
     * @return
     */
    @PostMapping("/getPurchaseOrdeInfoById/{purchaseOrdeId}")
    public ResponseAPI getPurchaseOrdeInfoById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        PurchaseOrderVO purchaseOrde = purchaseOrdeService.getPurchaseOrdeInfoById(purchaseOrdeId);
        return ResponseAPI.successData(purchaseOrde);
    }
    /**
     * 更具采购单号获取采购信息
     * @return
     */
    @PostMapping("/getPurOrdeInfoById/{purchaseOrdeId}")
    public ResponseAPI getPurOrdeInfoById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        List<PurchaseOrderVO>  purchaseOrderVOS  = purchaseOrdeService.getPurOrdeInfoById(purchaseOrdeId);
        return ResponseAPI.successData(purchaseOrderVOS);
    }

    @PostMapping("/queryPaymentPlanByPurchaseOrderID")
    public ResponseAPI queryPaymentPlanByPurchaseOrderID(@RequestBody PaymentPlan paymentPlan){
        PaymentPlan paymentPlanVo = paymentPlanService.queryPaymentPlanByPurchaseOrderID(paymentPlan);
        return ResponseAPI.successData(paymentPlanVo);
    }

    @PostMapping("/queryPayPlanByPidAndPlanPeriod")
    public ResponseAPI queryPayPlanByPidAndPlanPeriod(@RequestBody PaymentPlan paymentPlan){
        List<PaymentPlan> paymentPlans= paymentPlanService.queryPayPlanByPidAndPlanPeriod(paymentPlan);
        return ResponseAPI.successData(paymentPlans);
    }
    /**
     * 采购信息驳回审批
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/rejectionPurchaseOrderInfo")
    public ResponseAPI rejectionPurchaseOrderInfo(@RequestBody PurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        return ResponseAPI.successData(purchaseOrdeService.rejectionPurchaseOrderInfo(purchaseOrder, token));
    }

    /**
     * 采购信息通过审批
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/approvePurchaseOrderInfo")
    public ResponseAPI approvePurchaseOrderInfo(@RequestBody PurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        return ResponseAPI.successData(purchaseOrdeService.approvePurchaseOrderInfo(purchaseOrder, token,false));
    }

    /**
     * 通过申请表主键查询现有数据
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPurchaseOrderByApprovalId/{approvalId}")
    public ResponseAPI queryPurchaseOrderByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(purchaseOrdeService.queryPurchaseOrderByApprovalId(approvalId));
    }

    /**
     * 通过申请表主键查询历史
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPurchaseOrderHistoryByApprovalId/{approvalId}")
    public ResponseAPI queryPurchaseOrderHistoryByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(purchaseOrdeService.queryPurchaseOrderHistoryByApprovalId(approvalId));
    }

    @PostMapping("/savePayPlan")
    public ResponseAPI savePayPlan(@RequestBody PaymentPlan paymentPlan) {
        paymentPlanService.updatePayPlanById(paymentPlan);

        return ResponseAPI.successData(paymentPlan);
    }

    @PostMapping("/getPaymentPlanByPurchaseOrderID")
    public ResponseAPI getPaymentPlanByPurchaseOrderID(@RequestBody PaymentPlan paymentPlan){
        List<PaymentPlan>  paymentPlanVos = paymentPlanService.getPaymentPlanByPurchaseOrderID(paymentPlan);
        return ResponseAPI.successData(paymentPlanVos);
    }


    /**
     * 获取所有通过审批的采购单
     * @return
     */
    @PostMapping("getPurchaseById/{contractId}")
    public ResponseAPI getPurchaseById(@PathVariable(value = "contractId") int contractId){
        return ResponseAPI.successData(purchaseOrdeService.getPurchaseById(contractId));
    }

    @PostMapping("/getWmsProjectInfo/{contractId}")
    public ResponseAPI getWmsProjectInfo(@PathVariable(value = "contractId") int contractId){
        return ResponseAPI.successData(purchaseOrdeService.getWmsProjectInfo(contractId));
    }

    @PostMapping("/getAllContract/{projectManageId}")
    public ResponseAPI getAllContract(@PathVariable(value = "projectManageId") int projectManageId){
        return ResponseAPI.successData(purchaseOrdeService.getAllContract(projectManageId));
    }

    /**
     * 分页查新采购信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryPurchaseOrdesByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryPurchaseOrdesByPage( @PathVariable(value = "pageNum") int pageNum,
                                             @PathVariable(value = "pageSize") int pageSize,
                                             @RequestBody PurchaseOrderVO entity,
                                             @RequestHeader("token") String token) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = purchaseOrdeService.queryPurchaseOrdesByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    @PostMapping("/exportPurchaseOrdes")
    public void exportPurchaseOrdes(HttpServletResponse response,
                                   @RequestBody PurchaseOrderVO entit,
                                   @RequestHeader("token") String token) throws Exception{
        purchaseOrdeService.exportPurchaseOrdes(response,entit,token);
    }

    /**
     * 分页查新采购明细信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryPurchaseProductsByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryPurchaseProductsByPage( @PathVariable(value = "pageNum") int pageNum,
                                                 @PathVariable(value = "pageSize") int pageSize,
                                                 @RequestBody PurchaseOrderProductVO entity,
                                                 @RequestHeader("token") String token) {
        IPage<PurchaseOrderProductVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderProductVO> result = purchaseOrdeService.queryPurchaseProductsByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 导出采购明细数据
     */
    @PostMapping("/exportPurchaseProducts")
    public void exportPurchaseProducts(HttpServletResponse response,
                                    @RequestBody PurchaseOrderProductVO entit,
                                    @RequestHeader("token") String token) throws Exception{
        purchaseOrdeService.exportPurchaseProducts(response,entit,token);
    }


    /**
     * 分页查新付款计划
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryPaymentPlansByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryPaymentPlansByPage( @PathVariable(value = "pageNum") int pageNum,
                                                  @PathVariable(value = "pageSize") int pageSize,
                                                  @RequestBody PaymentPlan entity,
                                                  @RequestHeader("token") String token) {
        IPage<PaymentPlan> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PaymentPlan> result = paymentPlanService.queryPaymentPlansByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 导出付款计划
     * @param response
     * @param entit
     * @param token
     * @throws Exception
     */
    @PostMapping("/exportPaymentPlans")
    public void exportPaymentPlans(HttpServletResponse response,
                                     @RequestBody PaymentPlan entit,
                                     @RequestHeader("token") String token) throws Exception{
        paymentPlanService.exportPaymentPlans(response,entit,token);
    }

    /**
     * 更据采购ID获取采购相关付款 收票记录
     * @param purId
     * @return
     */
    @PostMapping("/queryPurPayInfoByPurId/{purId}")
    public ResponseAPI queryPurPayInfoByPurId(@PathVariable(value = "purId") int purId){
        return ResponseAPI.successData(purchaseOrdeService.queryPurPayInfoByPurId(purId));
    }


    /**
     * 更据项目ID获取采购相关付款 收票记录
     * @param proId
     * @return
     */
    @PostMapping("/queryPurPayInfoByProId/{proId}")
    public ResponseAPI queryPurPayInfoByProId(@PathVariable(value = "proId") int proId){
        return ResponseAPI.successData(purchaseOrdeService.queryPurPayInfoByProId(proId));
    }


    /**
     * 根据客户ID获取数据总和
     * @param customerId
     * @return
     */
    @PostMapping("/queryPurPayInfoByCustomerId/{customerId}")
    public ResponseAPI queryPurPayInfoByCustomerId(@PathVariable(value = "customerId") int customerId ,@RequestHeader("token") String token){
        return ResponseAPI.successData(purchaseOrdeService.queryPurPayInfoByCustomerId(customerId,token));
    }


    /**
     * 根据供应商ID获取数据总和
     * @param supplierId
     * @return
     */
    @PostMapping("/queryPurPayInfoBySupplierId/{supplierId}")
    public ResponseAPI queryPurPayInfoBySupplierId(@PathVariable(value = "supplierId") int supplierId ,@RequestHeader("token") String token){
        return ResponseAPI.successData(purchaseOrdeService.queryPurPayInfoBySupplierId(supplierId,token));
    }


    /**
     * 分页查询采购待办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurByPage( @PathVariable(value = "pageNum") int pageNum,
                                                 @PathVariable(value = "pageSize") int pageSize,
                                                 @RequestBody PurchaseOrderVO entity) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = purchaseOrdeService.queryApprovalPurByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询采购已办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurDoneByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurDoneByPage( @PathVariable(value = "pageNum") int pageNum,
                                               @PathVariable(value = "pageSize") int pageSize,
                                               @RequestBody PurchaseOrderVO entity) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = purchaseOrdeService.queryApprovalPurDoneByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询采购申请记录信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurRecordByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurRecordByPage( @PathVariable(value = "pageNum") int pageNum,
                                                     @PathVariable(value = "pageSize") int pageSize,
                                                     @RequestBody PurchaseOrderVO entity,
                                                     @RequestHeader("token") String token) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = purchaseOrdeService.queryApprovalPurRecordByPage(page ,entity,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 删除采购单
     * @param purchaseOrdeId
     * @return
     */
    @PostMapping("/deletePurchaseOrderById/{purchaseOrdeId}")
    public ResponseAPI deletePurchaseOrderById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        return ResponseAPI.successData(purchaseOrdeService.deletePurchaseOrderById(purchaseOrdeId));
    }

    /**
     * 分页查新采购信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryRefundDetailsByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryRefundDetailsByPage( @PathVariable(value = "pageNum") int pageNum,
                                                 @PathVariable(value = "pageSize") int pageSize,
                                                 @RequestBody PurchaseOrderVO entity) {
        IPage<PurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PurchaseOrderVO> result = purchaseOrdeService.queryRefundDetailsByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    @GetMapping("queryRefundCharData")
    public ResponseAPI queryRefundCharData() {
        return ResponseAPI.successData(purchaseOrdeService.queryRefundCharData());
    }

    /**
     * 导出退款详情
     */
    @PostMapping("/exportRefundDetails")
    public void exportRefundDetails(HttpServletResponse response, @RequestBody PurchaseOrderVO entit) throws Exception{
        purchaseOrdeService.exportRefundDetails(response, entit);
    }

    /**
     * 查询项目
     * @return
     */
    @PostMapping("/selectAllProjectByPur")
    public ResponseAPI selectAllProjectByPur(){
        return ResponseAPI.successData(purchaseOrdeService.selectAllProjectByPur());
    }


    /**
     * 查询项目
     * @return
     */
    @PostMapping("/selectAllProjectByPurCreate")
    public ResponseAPI selectAllProjectByPurCreate(@RequestHeader("token") String token){
        return ResponseAPI.successData(purchaseOrdeService.selectAllProjectByPurCreate(token));
    }

}
